home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Tools for Macintosh
/
Power Tools for Macintosh (SoftBit)(1992).iso
/
Applications
/
Alpha 4.01
/
ACMDS
/
wc ACMD.c
< prev
Wrap
C/C++ Source or Header
|
1990-12-01
|
2KB
|
141 lines
/*
wc.c
This ACMD by Jon Wätte gives a wc-like output for the selection.
Copyright © 1990 Jon Wätte (h+@nada.kth.se)
Permission granted to distribute FREELY by non-profit organizations
and individuals only.
*/
/* #define DEBUG /* */
void
_NumToString(long x, char * s)
{
char * len = s;
char * t, * q;
s++;
*len = 0;
if(x < 0) {
*(s++) = '-';
(*len)++;
}
q = s;
if(!x) {
*(s++) = '0';
(*len)++;
} else while(x) {
for(t = s; t > len; t--) *t = *(t-1);
*q = '0' + (x % 10);
x /= 10;
s++;
(*len)++;
}
}
void
PresentData(long a, long b, long c)
{
WindowPtr theWindow, saveWindow, frontPort;
Rect r;
char foo[256];
long time;
EventRecord theEvent;
GetPort(&frontPort);
saveWindow = FrontWindow();
SetRect(&r, 90, 90, 250, 170);
theWindow = NewWindow(0L, &r, "\PWord count", 1, 1, 0L, 0, 0L);
SelectWindow(theWindow);
SetPort(theWindow);
TextFont(0);
TextSize(12);
TextFace(0);
MoveTo(20, 20);
DrawString("\PLines:");
MoveTo(20, 40);
DrawString("\PWords:");
MoveTo(20, 60);
DrawString("\PChars:");
_NumToString(a, foo);
MoveTo(100, 20);
DrawString(foo);
_NumToString(b, foo);
MoveTo(100, 40);
DrawString(foo);
_NumToString(c, foo);
MoveTo(100, 60);
DrawString(foo);
time = TickCount() + 600;
do {
GetNextEvent(mDownMask + keyDownMask, &theEvent);
} while(theEvent.what != keyDown && theEvent.what != mouseDown &&
TickCount() < time);
DisposeWindow(theWindow);
SelectWindow(saveWindow);
SetPort(frontPort);
}
char *
#ifdef DEBUG
_main
#else
main
#endif
(char * inData)
{
register char * data = inData;
register long wordcnt = 0;
register long rowcnt = 1;
register long flag = 1;
while(*data) {
switch(*data) {
case '\r':
rowcnt++;
case ' ':
case '\t':
wordcnt += flag;
flag = 0;
break;
default:
flag = 1;
break;
}
data++;
}
if ((*(data - 1) != ' ') && (*(data - 1) != '\t') && (*(data - 1) != '\r'))
wordcnt++;
PresentData(rowcnt, wordcnt, data - inData - (rowcnt - 1));
DisposPtr(inData);
return 0L;
}
#ifdef DEBUG
main()
{
InitGraf(&thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
_main("Foo bar i skogen.\rJag undrar vad som h{nder \r om man...");
}
#endif